# Copyright (C) 2014-2015 Free Software Foundation, Inc.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

# Test basic builtin types.
# NOTE: The tests here intentionally do not require a D compiler.

load_lib "d-support.exp"

if { [skip_d_tests] } { continue }

proc test_d_integer_literals {} {
    # Test valid D integer literals are accepted.

    gdb_test "print 123456" " = 123456"
    gdb_test "print 123_456" " = 123456"
    gdb_test "print 1_2_3_4_5_6_" " = 123456"

    gdb_test "print 0x123456" " = 1193046"
    gdb_test "print 0x123_456" " = 1193046"
    gdb_test "print 0x1_2_3_4_5_6_" " = 1193046"

    gdb_test "print 0123456" " = 42798"
    gdb_test "print 0123_456" " = 42798"
    gdb_test "print 01_2_3_4_5_6_" " = 42798"

    gdb_test "print 0b101010" " = 42"
    gdb_test "print 0b101_010" " = 42"
    gdb_test "print 0b1_0_1_0_1_0_" " = 42"

    # Usual decimal notation
    gdb_test "ptype 0" "type = int"
    gdb_test "ptype 2_147_483_647" "type = int"
    gdb_test "ptype 2_147_483_648" "type = long"
    gdb_test "ptype 4_294_967_296" "type = long"

    # Explicit suffixes
    gdb_test "ptype 0L" "type = long"
    gdb_test "ptype 2_147_483_648U" "type = uint"
    gdb_test "ptype 4_294_967_296U" "type = ulong"
    gdb_test "ptype 0UL" "type = ulong"
    gdb_test "ptype 0LU" "type = ulong"

    # Hexadecimal notation
    gdb_test "ptype 0x0" "type = int"
    gdb_test "ptype 0x7FFF_FFFF" "type = int"
    gdb_test "ptype 0x8000_0000" "type = uint"
    gdb_test "ptype 0x1_0000_0000" "type = long"

    # Hexadecimal notation with explicit suffixes
    gdb_test "ptype 0x0L" "type = long"
    gdb_test "ptype 0x7FFF_FFFFU" "type = uint"
    gdb_test "ptype 0x1_0000_0000U" "type = ulong"
    gdb_test "ptype 0x0UL" "type = ulong"
    gdb_test "ptype 0x0LU" "type = ulong"

    # Octal notation
    gdb_test "ptype 00" "type = int"
    gdb_test "ptype 017_777_777_777" "type = int"
    gdb_test "ptype 020_000_000_000" "type = uint"
    gdb_test "ptype 040_000_000_000" "type = long"

    # Octal notation with explicit suffixes
    gdb_test "ptype 00L" "type = long"
    gdb_test "ptype 017_777_777_777U" "type = uint"
    gdb_test "ptype 040_000_000_000U" "type = ulong"
    gdb_test "ptype 00UL" "type = ulong"
    gdb_test "ptype 00LU" "type = ulong"

    # Binary notation
    gdb_test "ptype 0b0" "type = int"
    gdb_test "ptype 0b1111111111111111111111111111111" "type = int"
    gdb_test "ptype 0b10000000000000000000000000000000" "type = uint"
    gdb_test "ptype 0b100000000000000000000000000000000" "type = long"

    # Binary notation with explicit suffixes
    gdb_test "ptype 0b0L" "type = long"
    gdb_test "ptype 0b1111111111111111111111111111111U" "type = uint"
    gdb_test "ptype 0b100000000000000000000000000000000U" "type = ulong"
    gdb_test "ptype 0b0UL" "type = ulong"
    gdb_test "ptype 0b0LU" "type = ulong"
}

proc test_d_float_literals {} {
    # Test valid D float literals are accepted.

    gdb_test "ptype 123_456.567_8" "type = double"
    gdb_test "ptype 1_2_3_4_5_6_._5_6_7_8" "type = double"
    gdb_test "ptype 1_2_3_4_5_6_._5e-6_" "type = double"
    gdb_test "ptype 0x1.FFFFFFFFFFFFFp1023" "type = double"
    gdb_test "ptype 0x1p-52L" "type = real"
    gdb_test "ptype 1.175494351e-38F" "type = float"
    gdb_test "ptype 6.3i" "type = idouble"
    gdb_test "ptype 6.3fi" "type = ifloat"
    gdb_test "ptype 6.4Li" "type = ireal"
}

proc test_d_expressions {} {
    # Test expression behaviour specific to D.

    # Comparison and order expressions have same precedence.
    gdb_test "print 1 == 2 > 0" "A syntax error in expression, near `> 0'\."
    gdb_test "print (1 == 2) > 0" " = false"

    # Exponent expressions
    gdb_test "print 5 ^^ 5" "3125"
    gdb_test "print 144 ^^ 0.5" "12"
    gdb_test "print (-10 ^^ 2)" "-100"
    gdb_test "print (-10) ^^ 2" "100"

    gdb_test_no_output "set \$var = 144 ^^ 0.5" ""
    gdb_test "print \$var ^^= 2" "144"
}

# Start with a fresh gdb.

gdb_exit
gdb_start

if [set_lang_d] {
    test_d_integer_literals
    test_d_float_literals
    test_d_expressions
} else {
    warning "D type tests suppressed."
}
